﻿using RTW.Core.Ioc;
using RTW.Core.Repository;
using RTW.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RTW.Servers
{
    public class RoleServers : BaseServers<SysRole>, IAutoInject
    {
        private readonly IRepositoryAsync<SysRole> SysRoleRepository;
        public RoleServers()
        {
            SysRoleRepository = new DapperAsyncRepository<SysRole>();
        }


        public async Task<List<SysRole>> GetAllRoles()
        {
            return (await SysRoleRepository.GetAllEnumerableAsync()).ToList();
        }

        public async Task<SysRole> GetRole(int? id)
        {
            return await SysRoleRepository.GetByIdAsync(id);
        }


        /// <summary>
        /// 根据单位ID获取数据
        /// </summary>
        /// <param name="DWID"></param>
        /// <returns></returns>
        public async Task<List<SysRole>> GetRoleWhere(int? DWID)
        {
            return (await SysRoleRepository.SqlWithParams("select * from Sys_Role  where DWID=:DWID", new { DWID = DWID })).ToList();
        }
        
        public async Task<List<SysRole>> GetRoleIDWhere(int? RID)
        {
            return (await SysRoleRepository.SqlWithParams("select * from Sys_Role  where RID=:RID", new { RID = RID })).ToList();
        }
        public async Task<bool> AnyByName(object name)
        {
            return await SysRoleRepository.SqlWithParamsBool("select * from Sys_Role  where RMC=:RMC", new { RMC = name });
        }

        public async Task<bool> AnyByCode(string code)
        {
            return await SysRoleRepository.SqlWithParamsBool("select * from Sys_Role  where DWDM=:DWDM", new { DWDM = code });
        }

        /// <summary>
        /// 根据用户ID得到对应角色名
        /// </summary>
        /// <param name="yhid"></param>
        /// <returns></returns>
        public async Task<List<SysRole>> GetRolename(int yhid)
        {
            return (await SysRoleRepository.SqlWithParams("select RMC from Sys_Role a join Sys_User_Role  b on a.rid=b.rid and b.yhid=:yhid",new { yhid=yhid})).ToList();
        }
    }
}
